class Solution
{
public:
    int numRookCaptures(vector<vector<char>> &board)
    {
        int x = 0;
        int y = 0;
        while (y < 8)
        {
            if (board[x][y] == 'R')
            {
                break;
            }
            ++x;
            y += x / 8;
            x %= 8;
        }
        auto catchDirection = [&](int dx, int dy) -> bool
        {
            int posX = x;
            int posY = y;
            while (true)
            {
                posX += dx;
                posY += dy;
                if (posX < 0 || posX >= 8 || posY < 0 || posY >= 8)
                {
                    return false;
                }
                if (board[posX][posY] == 'p')
                {
                    return true;
                }
                else if (board[posX][posY] == 'B')
                {
                    return false;
                }
            }
            return false;
        };
        int canCatch = 0;
        canCatch += catchDirection(1, 0);
        canCatch += catchDirection(-1, 0);
        canCatch += catchDirection(0, 1);
        canCatch += catchDirection(0, -1);
        return canCatch;
    }
};